//
//  CollectViewController.m
//  A段项目
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "CollectViewController.h"
#import "SXLineLayout.h"
#import "Model.h"
#import  <UIImageView+WebCache.h>
#import "FMDBDataHandler.h"
#import "Person.h"
#import "ChoosePictureCell.h"
#import "SXStackLayout.h"

@interface CollectViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrPic;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) ChoosePictureCell *cell;



@end

@implementation CollectViewController

{
    
    BOOL stateOfLayout;
}

- (void)dealloc {
    
    [_cell release];
    [_arrPic release];
    
    [_collection release];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"collectCount" object:nil];
    
    
    [super dealloc];
    
  
  
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self dataArray];
    [self handleData];
    
    [self createCollection];
    
    self.navigationItem.title = [NSString stringWithFormat:@"收藏的图片(%ld)",self.arrPic.count];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:1 target:self action:@selector(deleAction)];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
}

- (NSArray *)dataArray {
    
    if (self.arrPic == nil) {
        
        self.arrPic = [NSMutableArray array];
        
        return  self.arrPic;
        
    }else {
        
        
        return self.arrPic;
    }
    
}

- (void)handleData {
    
  /**数据库查询数据.*/
    
    NSArray *arr = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:200];
    
    for (int i = 0; i < arr.count; i ++) {
        
        Person *per = [arr objectAtIndex:i];
        
        [self.arrPic addObject:per];
        
    }
        
    
}

- (void)createCollection {
    
    
    SXStackLayout *layout = [[SXStackLayout alloc] init];
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    
    
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collection];
    
    [self.collection registerClass:[ChoosePictureCell class] forCellWithReuseIdentifier:@"pool1"];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.backgroundColor =[UIColor blackColor];
    
    [_collection release];
    
    [layout release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrPic.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
  
    
 self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    Person *per = [self.arrPic objectAtIndex:indexPath.row];
    
    [self.cell passedInfo:[UIImage imageWithData:per.data]];

    self.cell.button.hidden = YES;
   
    self.cell.layer.borderWidth = 5;
    
    self.cell.layer.borderColor = [[UIColor blueColor]CGColor];
    
    self.cell.layer.masksToBounds = YES;
    
    self.cell.layer.cornerRadius = 5;
    
    return self.cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    Model *model = [self.arrPic objectAtIndex:indexPath.item];
    
    [self.arrPic addObject:model];
    
    [self.arrPic removeObjectAtIndex:0];
    
    
    
    [self.collection reloadData];
    
    
}

- (void)deleAction {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"collectCount" object:nil userInfo:@{@"number":[NSNumber numberWithInteger:self.arrPic.count]}];
    
    
    
    if (self.arrPic.count != 0) {
        
        Person *per = [self.arrPic objectAtIndex:0];
        
        
        
        [[FMDBDataHandler shareDataBaseHandler]deleteImage:per.name];
        
       
        [self.arrPic removeObjectAtIndex:0];
        
        [self.collection reloadData];
        
        self.navigationItem.title = [NSString stringWithFormat:@"收藏的图片(%ld)",self.arrPic.count];
        
        [self.collection reloadData];
        
    }else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已经没有收藏的照片了" preferredStyle:1];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了!" style:1 handler:nil];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
